package com.powergisol.gis.util;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class FileUtil {
    private static Logger logger = LoggerFactory.getLogger(FileUtil.class);

    public static List<File> recursionFile(String recursionPath, List<File> list) {
        if(null == list){
            list = new ArrayList<>();
        }
        Map<String,Object> map = new HashMap<>();
        File file = new File(recursionPath);
        if (file.exists()) {
            File[] files = file.listFiles();
            if (null != files) {
                for (File file1 : files) {
                    if (file1.isDirectory()) {
                        recursionFile(file1.getAbsolutePath(),list);
                    } else {
                        list.add(file1);
                    }
                }
            }
        }
        return list;
    }

    public static String getContent(String pathname){
        if(StringUtils.isBlank(pathname)){
            return null;
        }
        File file = new File(pathname); // 要读取以上路径的output.txt文件
        if(file.isFile() && file.exists()){
            try {
                FileInputStream fileInputStream = new FileInputStream(file);
                InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                StringBuffer sb = new StringBuffer();
                String text = null;
                while((text = bufferedReader.readLine()) != null){
                    sb.append(text);
                }
                return sb.toString();
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
        return null;
    }
    public static String getDatafromFile(InputStream in) {
        BufferedReader reader = null;
        String laststr = "";
        try {
            InputStreamReader inputStreamReader = new InputStreamReader(in, "UTF-8");
            reader = new BufferedReader(inputStreamReader);
            String tempString = null;
            while ((tempString = reader.readLine()) != null) {
                laststr += tempString;
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return laststr;
    }

}
